﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Team 的摘要说明
/// </summary>
public class Team : LwUser
{
    public int DoctorNum;
    public int StudentNum;
    public int ExpertNum;
    public int PaperNum;
    public string Name;
    public string _Yjfx;
    public string Yjfx
    {
        set
        {
            string s = value;
            string sql = "update Doctor Set Yjfx='"+Fun.L(s)+"' where TeamGuid="+this.Guid.ToString()+" and Yjfx='"+Fun.L(_Yjfx)+"'";
            Fun.Execute(sql);
            this._Yjfx = value;
        }
        get
        {
            return _Yjfx;
        }
    }
    public Team(int g)
        : base(g)
	{
        this.Init();
    }
    public Team(string uid)
        : base(uid)
    {
        this.Init();
    }

    private void Init()
    {
        string sql = "select * from Team where UserGuid=" + this.Guid.ToString();
        DataRow dr = Fun.CreateDr(sql);
        this.Name = dr["TeamName"].ToString().Trim();
        this._Yjfx = dr["Yjfx"].ToString().Trim();
        sql = "select count(*) as n from Doctor where TeamGuid=" + this.Guid.ToString();
        this.DoctorNum = Convert.ToInt32(Fun.GetField(sql));
        sql = "select count(*) as n from Doctor where Expert=1 and TeamGuid=" + this.Guid.ToString();
        this.ExpertNum = Convert.ToInt32(Fun.GetField(sql));
        sql = "select count(*) as n from Student where DoctorGuid in (select Guid from Doctor where TeamGuid=" + this.Guid.ToString() + ")";
        this.StudentNum = Convert.ToInt32(Fun.GetField(sql));
        sql = "select count(*) as n from Relationship where Relationship='" + Paper.StrPaper + "' and GuidOne in (select Guid from Doctor where TeamGuid=" + this.Guid.ToString() + ")";
        this.PaperNum = Convert.ToInt32(Fun.GetField(sql));
    }

    public int PaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid in (select UserGuid from Doctor where TeamGuid=" + this.Guid.ToString() + ")";
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int PastPaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid in (select UserGuid from Doctor where TeamGuid=" + this.Guid.ToString() + ") and PaperFlag=" + Paper.PastFlag.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int SuspendPaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid in (select UserGuid from Doctor where TeamGuid=" + this.Guid.ToString() + ") and PaperFlag=" + Paper.SuspendFlag.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }


    public int FailPaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid in (select UserGuid from Doctor where TeamGuid=" + this.Guid.ToString() + ") and PaperFlag=" + Paper.FailFlag.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public override bool Update()
    {
        base.Update();
        string sql = "update Team set TeamName='"+Fun.L(this.Name)+"',Yjfx='"+Fun.L(this.Yjfx)+"' where UserGuid="+this.Guid.ToString();
        return Fun.Execute(sql)>0;
    }

    public static int Count()
    {
        string sql = "select count(*) as n from Team";
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static bool IsTeam(int g)
    {
        string sql = "select UserType from Users where Guid=" + g.ToString();
        object obj = Fun.GetField(sql, "UserType");
        if (obj == null)
        {
            return false;
        }
        return obj.ToString().Trim() == LwUser.TeamType;
    }

    public static int Add(string uid, string tn, string email,string y,ref string tip)
    {
        int g = LwUser.Add(uid, tn, email, LwUser.TeamType, ref tip);
        if (g == 0)
        {
            return 0;
        }
        string sql = "insert into Team(Guid,TeamName,Yjfx,UserGuid) values(" + Fun.Guid().ToString() + ",'" + Fun.L(tn) + "','" + Fun.L(y) + "',"+g.ToString()+")";
        if (Fun.Execute(sql) > 0)
        {
            return g;
        }
        else
        {
            tip = "添加失败！";
            sql = "delete from Users where Guid=" + g.ToString();
            Fun.Execute(sql);
            return 0;
        }
        return g;
    }

    public static DataTable TeamsDt()
    {
        string sql = "select UserGuid from Team order by Guid asc";
        return Fun.CreateDt(sql);
    }

    public static DataTable Teams(bool hasAll)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("value", Params.IntType);
        dt.Columns.Add("text", Params.StrType);
        DataRow dr ;
        if (hasAll)
        {
            dr = dt.NewRow();
            dr["text"] = "所有团队";
            dr["value"] = 0;
            dt.Rows.Add(dr);
        }
        string sql = "select UserGuid,TeamName from Team order by Guid asc";
        DataTable dt2 = Fun.CreateDt(sql);
        foreach (DataRow dr2 in dt2.Rows)
        {
            dr = dt.NewRow();
            dr["text"] = dr2["TeamName"].ToString().Trim();
            dr["value"] = Convert.ToInt32(dr2["UserGuid"]);
            dt.Rows.Add(dr);
        }
        return dt;
    }

    public static void Delete(int g)
    {
        string sql = "select UserGuid from Doctor where TeamGuid="+g.ToString();
        DataTable dt = Fun.CreateDt(sql);
        foreach (DataRow dr in dt.Rows)
        {
            Doctor.Delete(Convert.ToInt32(dr[0]));
        }
        sql = "delete from Team where UserGuid="+g.ToString();
        Fun.Execute(sql);
        LwUser.Delete(g);
    }

    public static Team Check()
    {
        try
        {
            LwUser lu = (LwUser)System.Web.HttpContext.Current.Session["Loginer"];
            string uid = System.Web.HttpUtility.UrlDecode(System.Web.HttpContext.Current.Request.Params["uid"]);
            if (lu.UserType == LwUser.AdminType)
            {
                return new Team(uid);
            }
            if (lu.UserType == LwUser.TeamType)
            {
                if (lu.UserID != uid)
                {
                    return null;
                }
                return new Team(uid);
            }
            return null;
        }
        catch
        {
            return null;
        }
    }

    public static bool TeamHasDoctor(int tg, int dg)
    {
        string sql = "select count(*) as n from Doctor where UserGuid="+dg.ToString()+" and TeamGuid="+tg.ToString();
        int c = Convert.ToInt32(Fun.GetField(sql));
        return c > 0;
    }

    public static bool TeamHasDoctor(int tg, string duid)
    {
        string sql = "select count(*) as n from Doctor d JOIN Users u ON d.UserGuid=u.Guid where u.UserID='"+Fun.L(duid.Trim())+"' and d.TeamGuid=" + tg.ToString();
        int c = Convert.ToInt32(Fun.GetField(sql));
        return c > 0;
    }
}
